﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BotonJXC.Entity;

namespace BotonJXC.DAO
{
    public class SysContext
    {
        private static IList<Warehouse> _userWarehouse;
        /// <summary>
        /// 获取或设置当前用户数据权限
        /// </summary>
        public static IList<RoleDataAuth> CurrentUserDataAuth { get; set; }
        /// <summary>
        /// 获取或设置当前用户资源功能权限
        /// </summary>
        public static IList<AuthRes> CurrentUserResAuth { get; set; }

        /// <summary>
        /// 获取或设置当前用户可访问的库存地
        /// </summary>
        public static IList<Warehouse> CurrentUserWarehouses { get; set; }
        /// <summary>
        /// 检查当前用户是否有某个资源功能权限
        /// </summary>
        /// <param name="authResSn"></param>
        /// <returns></returns>
        public static bool CheckCurrentUserResAuth(string authResSn)
        {
            foreach (AuthRes ar in CurrentUserResAuth)
            {
                if (authResSn.Equals(ar.AuthResSn))
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 判断当前用户是否有某个库存地的权限
        /// </summary>
        /// <param name="warehouseId"></param>
        /// <returns></returns>
        public static bool CheckWarehouse(int warehouseId)
        {
            if (CurrentUserWarehouses != null)
            {
                foreach (Warehouse w in CurrentUserWarehouses)
                {
                    if (w.WarehouseId == warehouseId)
                        return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 获取当前用户数据权限中所有部门ID,排除拥有某个门店所有部门的权限的ID
        /// </summary>
        /// <returns></returns>
        public static int[] GetCurrentUserDataAuthDepId()
        {
            List<int> depIds = new List<int>();
            foreach (RoleDataAuth rda in CurrentUserDataAuth)
            {
                if(rda.DepId != 0)
                    depIds.Add(rda.DepId);
            }
            return depIds.ToArray();
        }



        /// <summary>
        /// 获取当前用户数据权限中所有门店ID
        /// </summary>
        /// <returns></returns>
        public static int[] GetCurrentUserDataAuthStoreId()
        {
            List<int> storeIds = new List<int>();
            foreach (RoleDataAuth rda in CurrentUserDataAuth)
            {
                if(rda.DepId == 0)
                storeIds.Add(rda.StoreId);
            }
            return storeIds.ToArray();
        }
    }
}
